/**
 *                            License
 * THE WORK (AS DEFINED BELOW) IS PROVIDED UNDER THE TERMS OF THIS  
 * CREATIVE COMMONS PUBLIC LICENSE ("CCPL" OR "LICENSE"). 
 * THE WORK IS PROTECTED BY COPYRIGHT AND/OR OTHER APPLICABLE LAW.  
 * ANY USE OF THE WORK OTHER THAN AS AUTHORIZED UNDER THIS LICENSE OR  
 * COPYRIGHT LAW IS PROHIBITED.
 * 
 * BY EXERCISING ANY RIGHTS TO THE WORK PROVIDED HERE, YOU ACCEPT AND  
 * AGREE TO BE BOUND BY THE TERMS OF THIS LICENSE. TO THE EXTENT THIS LICENSE  
 * MAY BE CONSIDERED TO BE A CONTRACT,
 * THE LICENSOR GRANTS YOU THE RIGHTS CONTAINED 
 * HERE IN CONSIDERATION OF YOUR ACCEPTANCE OF SUCH TERMS AND CONDITIONS.
 * 
 */
package com.lineage.server.model.item.etcitem.action;

import com.lineage.server.PacketCreate;
import com.lineage.server.model.Instance.L1ItemInstance;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.model.item.ItemExecutor;
import com.lineage.server.packetserver.S_ServerMessage;

/**
 * Class <code>ScrollProtectArmor</code> 装备保护卷轴 - 41434.
 * 
 * @author jrwz
 * @version 2012-7-11下午01:48:42
 * @see com.lineage.server.model.item.etcitem.action
 * @since JDK1.7
 */
public final class ScrollProtectArmor extends ItemExecutor {

    /**
     * 取得实例.
     * 
     * @return 实例
     */
    public static ItemExecutor get() {
        return new ScrollProtectArmor();
    }

    /** 装备保护卷轴 - 41434. */
    private ScrollProtectArmor() {
    }

    @Override
    public void execute(final L1PcInstance pc, final L1ItemInstance useItem,
            final int readC1, final int readC2, final int targetId,
            final int targetX, final int targetY) {
        final int itemobj = targetId;
        final L1ItemInstance l1iteminstance1 = pc.getInventory().getItem(
                itemobj);
        if (l1iteminstance1 != null) {
            // 无法保护道具类
            if (l1iteminstance1.getItem().getType2() == 0) {
                PacketCreate.get().sendSinglePackets(pc, "没有任何事情发生");
                return;
            }
            // 受保护中
            if (l1iteminstance1.isProtect()) {
                PacketCreate.get().sendSinglePackets(pc, "神秘力量的影响正在发生。");
                return;
            }
            // 不能加成无法保护
            final int safeenchant = l1iteminstance1.getItem().getSafeenchant();
            if (safeenchant <= -1) {
                PacketCreate.get().sendSinglePackets(pc, "此装备无法使用装备保护卷轴。");
                return;
            }
            // +12以上无法保护（含12）
            final int enchantLevel = l1iteminstance1.getEnchantLevel();
            if (enchantLevel >= 12) {
                PacketCreate.get().sendSinglePackets(pc, "此装备上是无法使用魔法的。");
                return;
            }
            l1iteminstance1.setProtect(true);
            // %0受到魔法力量的保护。
            pc.sendPackets(new S_ServerMessage(1308, l1iteminstance1
                    .getLogName()));
            pc.getInventory().removeItem(useItem, 1);
        }
    }
}
